package org.stvd.repository.common.impl;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.stvd.entities.common.MsgSendHis;
import org.stvd.repository.base.impl.BaseDaoImpl;
import org.stvd.repository.common.MsgSendHisDao;

@Repository("MsgSendHisDao")
public class MsgSendHisDaoImpl extends BaseDaoImpl<MsgSendHis> implements MsgSendHisDao{
    /**
     * 获取短信发送次数
     * @param dateStr 发送日期，格式如：yyyy-MM-dd
     * @param receiverNum 接收短信的号码
     * @param useFor 短信用途描述
     * @return
     */
    public int countSendTimesTheDay(String dateStr, String receiverNum,
            String useFor) {
        StringBuffer sql = new StringBuffer();
        sql.append("select count(his_id) from msg_send_his msh where 1=1 ");
        if (dateStr!=null && !"".equals(dateStr)) {
            sql.append("and date(msh.send_time) =:dateStr ");
        }
        if (receiverNum!=null && !"".equals(receiverNum)) {
            sql.append("and msh.receiver_no=:receiverNum ");
        }
        if (useFor!=null && !"".equals(useFor)) {
            sql.append("and msh.use_for=:useFor ");
        }

        Query query = em.createNativeQuery(sql.toString());
        if (dateStr!=null && !"".equals(dateStr)) {
            query.setParameter("dateStr", dateStr);
        }
        if (receiverNum!=null && !"".equals(receiverNum)) {
            query.setParameter("receiverNum", receiverNum);
        }
        if (useFor!=null && !"".equals(useFor)) {
            query.setParameter("useFor", useFor);
        }

        return Integer.parseInt(query.getSingleResult().toString());
    }
}
